package com.binhai.role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.binhai.db.SpringDB;

@Component
public class RoleDao {
    @Autowired
    private SpringDB springDB;

    public List getAllInfos() {
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_role where  state=0";
            //使用模版查询，返回list列表
            List list = jdbcTemplate.queryForList(sql);
            //响应数据
            return list;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Map saveInfo(RoleBean role) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "insert into t_role(name,remark,state) values('"+role.getName()+"','"+role.getRemark()+"',0)";
            jdbcTemplate.update(sql);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }

    public Map deleteInfo(int id) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "update t_role set state=1 where id="+id;
            jdbcTemplate.execute(sql);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }

    public Map getInfoById(int id) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_role where id="+id;
            map = jdbcTemplate.queryForMap(sql);
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }

    }

    public Map updateInfo(RoleBean role) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "update t_role set name='"+role.getName()+"',remark='"+role.getRemark()+"' where id="+role.getId();
            jdbcTemplate.execute(sql);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }
}
